const app = getApp();
import * as echarts from '../../ec-canvas/echarts';
function initChart(canvas, width, height) {
    const chart = echarts.init(canvas, null, {
      width: width,
      height: height
    });
    app.getPieCallback = res => {
      canvas.setChart(chart);
     // console.log('查看得到的数据:', res)
      var option = {
        backgroundColor: "#ffffff",
        color: ["#37A2DA", "#32C5E9", "#c2ce07", "#8e48c0", "#FF9F7F"],
        series: [{
          label: {
            normal: {
              fontSize: 14
            }
          },
          type: 'pie',
          center: ['50%', '50%'],
          radius: [0, '60%'],
          data: res,
          label: {
            normal: {
              fontSize: 14,
              rich: {}
            }
          },
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 2, 2, 0.3)'
            }
          }
        }]
      };
      chart.setOption(option, true);
      return chart;
     }  

  setTimeout(function () {
    canvas.setChart(chart);
    var option = {
      backgroundColor: "#ffffff",
      color: ["#37A2DA", "#32C5E9", "#c2ce07", "#8e48c0", "#FF9F7F"],
      series: [{
        label: {
          normal: {
            fontSize: 14
          }
        },
        type: 'pie',
        center: ['50%', '50%'],
        radius: [0, '60%'],
        data: app.globalData.piechart,
        label: {
          normal: {
            fontSize: 14,
            rich: {}
          }
        },
        itemStyle: {
          emphasis: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 2, 2, 0.3)'
          }
        }
      }]
    };


    chart.setOption(option, true);
    return chart;

    
  }, 1000) 

}


Page({

  data: {
    ec: {
      onInit: initChart
    },
    type:3,
    piechart:[],
    nickname:"",
    listData: [],
    YesCanvas:true,//有图表数据时
    NoCanvas: false,// 没有图表数据时

  },
  goSurveyTap:function(event){
    var that = this;
    var uid = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../survey/survey?id=' + uid + '&name=' + name,
    })
  },
  goOrderTap: function(event){
    var that = this;
    var oid = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../source/source?id=' + oid + '&name=' + name,
    })
  },
  /**
   * 页面的初始数据
   */


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var _this = this;
    // 获取游戏产品ID和名称 获取贴纸详情
    var uid = options.id;
    var name = options.name;
    var type = _this.data.type;
    _this.setData({
      uid: uid,
      name: name
    })

    this.requestApi(uid, type);

   // console.log(type)

    


  },
  weekTap:function(event){
    var _this = this;
    var uid = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    var type = event.currentTarget.dataset.type;
    _this.setData({
      type: type
    })
   // console.log(type)
    this.requestApi(uid, type);

    setTimeout(function () {
      app.PieChartData();
    }, 500) 

 
  },

  requestApi:function(uid,type){
    var _this = this;
    //  wx.clearStorage({
    //    success: function (res) {
    //      console.log(res + "清除缓存成功！")
    //    }
    //  })
    wx.request({
      url: 'https://data1.cqhot.cn/index.php?s=/Admin/Apiwx/detail',
      data: {
        user_id: uid,
        type: type
      },
      success: function (res) {
        if (res.data.code == 1) {
          if (res.data.data.user.portrait == null) {
            _this.setData({
              portrait: "../../images/headimg.jpg",
            })
          } else {
            _this.setData({
              portrait: res.data.data.user.portrait,
              nickname: res.data.data.user.nickname
            })
          }
          _this.setData({
            piechart: res.data.data.piechart,
            listData: res.data.data.list
          })

          var piechart = res.data.data.piechart
          app.globalData.piechart = res.data.data.piechart;
          //判断是否有图表数据
          if (piechart==""){
            _this.setData({
              YesCanvas: false,
              NoCanvas: true,
            })
          }else{
            _this.setData({
              YesCanvas: true,
              NoCanvas: false,
            })
          }
          // wx.setStorageSync('piechart', app.globalData.piechart);//缓存图表数据
          
        } else {
          console.log("请求失败!");
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: 'CRM助手',
      path: '/pages/survey/survey',
      success: function () { },
      fail: function () { }
    }
  },
})